package com.leilei.serviceProject.domain.strategy.service.rule.chain.impl;

import com.leilei.serviceProject.domain.strategy.model.entity.RaffleFactorEntity;
import com.leilei.serviceProject.domain.strategy.repository.IStrategyRepository;
import com.leilei.serviceProject.domain.strategy.service.rule.chain.AbstractLogicChain;
import com.leilei.serviceProject.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yanglei
 * @description
 * @create 2024/11/12 23:37
 */
@Slf4j
@Component("rule_blacklist")
public class BlackListLogicChain extends AbstractLogicChain {

    @Resource
    private IStrategyRepository strategyRepository;

    @Override
    public Integer logic(RaffleFactorEntity raffleFactorEntity) {
        String blackListStrategyRuleValue = strategyRepository.queryStrategyRuleEntity(raffleFactorEntity.getStrategyId(), raffleFactorEntity.getAwardId(),
                ruleModel());
        // ["100", "user001,user002,user003"]
        String[] blackListStrategyRuleValues = blackListStrategyRuleValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(blackListStrategyRuleValues[0]);
        String[] blackUserArray = blackListStrategyRuleValues[1].split(Constants.SPLIT);

        for (String blackUser : blackUserArray) {
            if (raffleFactorEntity.getUserId().equals(blackUser)) {
                log.info("抽奖责任链-黑名单接管:strategyId:{},userId:{},ruleModel:{},award:{}",raffleFactorEntity.getStrategyId()
                        ,raffleFactorEntity.getUserId(),ruleModel(),awardId);
                return awardId;
            }
        }

        log.info("抽奖责任链-黑名单放行:strategyId:{},userId:{},ruleModel:{}",raffleFactorEntity.getStrategyId(),
                raffleFactorEntity.getUserId(),ruleModel());

        return next().logic(raffleFactorEntity);
    }

    @Override
    protected String ruleModel() {
        return "rule_blacklist";
    }
}
